﻿using System;
using TweetSharp.Model;
using TweetSharp.Twitter.Extensions;
using TweetSharp.Twitter.Fluent;
using TweetSharp.Twitter.Model;
using TweetSharp.Twitter.Service;

namespace O1.Social.Twitter
{
    public class TwitterConnect
    {
        private readonly string m_ConsumerKey;
        private readonly string m_ConsumerSecret;

        public TwitterConnect(string consumerKey, string consumerSecret)
        {
            m_ConsumerKey = consumerKey;
            m_ConsumerSecret = consumerSecret;
        }

        

        public string GetResponse(TwitterResult response)
        {
            var screenName = "";

            var identity = response.AsUser();
            if (identity != null)
            {
                screenName = identity.ScreenName;

                //trace.InnerHtml = String.Format("{0} authenticated successfully.", identity.ScreenName);
            }

            var error = response.AsError();
            if (error != null)
            {
                throw new Exception(error.ErrorMessage);
                //trace.InnerHtml = error.ErrorMessage;
            }

            return screenName;
        }

        public OAuthToken GetAccessToken(string requestToken, string verifier)
        {
            var accessToken = FluentTwitter.CreateRequest()
                .Authentication.GetAccessToken(m_ConsumerKey, m_ConsumerSecret, requestToken, verifier);

            var response = accessToken.Request();
            var result = response.AsToken();

            if (result == null)
            {
                var error = response.AsError();
                if (error != null)
                {
                    throw new Exception(error.ErrorMessage);
                }
            }

            return result;
        }

        public OAuthToken GetRequestToken(string callbackUrl)
        {
            var requestToken = FluentTwitter.CreateRequest()
                .Authentication.GetRequestToken(m_ConsumerKey, m_ConsumerSecret, callbackUrl);

            var response = requestToken.Request();
            var result = response.AsToken();

            if (result == null)
            {
                var error = response.AsError();
                if (error != null)
                {
                    throw new Exception(error.ErrorMessage);
                }
            }

            return result;
        }

        public string GetAuthoriseUrl(OAuthToken request)
        {
            var authorizeUrl = FluentTwitter.CreateRequest()
                   .Authentication.GetAuthorizationUrl(request.Token);

            return authorizeUrl;
        }

        public string GetUserName(string token, string tokenSecret)
        {
            // make an oauth-authenticated call with the access token,
            // and remember you need to persist this token for this user's auth
            ITwitterLeafNode query = FluentTwitter.CreateRequest()
                .AuthenticateWith(m_ConsumerKey,
                                  m_ConsumerSecret,
                                  token,
                                  tokenSecret)
                .Account()
                .VerifyCredentials()
                .AsXml();

            var request = query.Request();
            var response = GetResponse(request);
            return response;
        }

        public void PostMessage(string token, string tokenSecret, string message)
        {            
            var service = new TwitterService();
            service.AuthenticateWith(m_ConsumerKey, m_ConsumerSecret, token, tokenSecret);
            service.SendTweet(message);
        }

     

    }
}
